//
//  LYYRadioViewController.m
//  Icar
//
//  Created by dllo on 15/10/23.
//  Copyright © 2015年 小龙侠. All rights reserved.
//

#import "LYYRadioViewController.h"
#import "LYYPlayListViewController.h"
@interface LYYRadioViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic,retain)UIButton *button;
@property (nonatomic,retain)NSMutableArray *dataArray;
@property (nonatomic,assign)BOOL isUp;

@end

@implementation LYYRadioViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view setBackgroundColor:[UIColor whiteColor]];
    self.titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,WIDTH(375), HEIGHT(64))];
    [self.titleView setBackgroundColor:BLUECOLOR];
    
    UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH(150), HEIGHT(20), WIDTH(80), HEIGHT(30))];
    [nameLabel setText:@"汽车电台"];
    [nameLabel setTextAlignment:NSTextAlignmentCenter];
    [nameLabel setTextColor:[UIColor whiteColor]];
    [self.titleView addSubview:nameLabel];
    if (VIEW_HEIGHT == 736) {
        [nameLabel setFont:[UIFont systemFontOfSize:22.]];
    }else if (VIEW_HEIGHT == 667)
    {
        [nameLabel setFont:[UIFont systemFontOfSize:20.]];
    }else if (VIEW_HEIGHT == 568){
        [nameLabel setFont:[UIFont systemFontOfSize:18.]];
    }else if (VIEW_HEIGHT == 480){
        [nameLabel setFont:[UIFont systemFontOfSize:16.]];
    }
    [self.view addSubview:self.titleView];
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.button setFrame:CGRectMake(WIDTH(5), HEIGHT(20), WIDTH(30), HEIGHT(30))];
    [self.button setImage:[UIImage imageNamed:@"iconfont-fanhui"] forState:UIControlStateNormal];
    [self.titleView addSubview:self.button];
    [self.button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
    [self setUpCollectionView];
    
    self.dataArray = [[NSMutableArray alloc]init];
    [self headerRefresh];//下拉刷新
}

- (void)buttonClick{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)setUpCollectionView{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.minimumInteritemSpacing = 2;
    flowLayout.minimumLineSpacing = 2;
    flowLayout.sectionInset = UIEdgeInsetsMake(1, 1, 1, 1) ;
    flowLayout.itemSize = CGSizeMake(WIDTH(100), HEIGHT(150)) ;
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, WIDTH(375), HEIGHT(560)) collectionViewLayout:flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    [self.collectionView setBackgroundColor:[UIColor whiteColor]];
    [self.collectionView registerClass:[LYYRadioCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.collectionView setBackgroundColor:[LYYNightModeHandle shareNightModeHandle].nightModeColor];
    self.collectionView.showsVerticalScrollIndicator = NO;
    [self.collectionView reloadData];
}

#pragma mark - 解析数据
- (void)headerRefresh{
    __block LYYRadioViewController *view = self;
    [self.collectionView addLegendHeaderWithRefreshingBlock:^{
        [view getDataSource];
        _isUp = YES;
    }];
    [self.collectionView.header beginRefreshing];
}

- (void)getDataSource{
    
    NSString *url = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov5.0.0/News/newsmyradiolist-pm2-tp0.json"];
    if (ISCONENNCTED == YES) {
        if (_isUp == YES) {
            [self.dataArray removeAllObjects];
        }
        [AFNBlock getDataWithString:url block:^(id back) {
            for (NSDictionary *dic in [[back objectForKey:@"result"]objectForKey:@"list"]) {
                LYYRadioModel *model = [LYYRadioModel modelWithDictionary:dic];
                [self.dataArray addObject:model];
            }
            [self.collectionView reloadData];
            [self.collectionView.header endRefreshing];
        }];
    }
}

#pragma mark - collectionview的代理
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    LYYRadioCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    LYYRadioModel *model = [self.dataArray objectAtIndex:indexPath.row];
    [cell getModel:model];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    LYYPlayListViewController *list = [[LYYPlayListViewController alloc]init];
    [self.navigationController pushViewController:list animated:YES];
    LYYRadioModel *model = [self.dataArray objectAtIndex:indexPath.row];
    list.model = model;
    self.navigationController.tabBarController.tabBar.hidden = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
